package com.irony.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.irony.eduservice.entity.EduSubject;
import com.irony.eduservice.entity.excel.SubjectData;
import com.irony.eduservice.entity.subject.OneSubject;
import com.irony.eduservice.entity.subject.TwoSubject;
import com.irony.eduservice.listener.SubjectExcelListener;
import com.irony.eduservice.mapper.EduSubjectMapper;
import com.irony.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author zgm
 * @since 2020-04-19
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {


    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService eduSubjectService) {
        try {
            //文件输入流
            InputStream in = file.getInputStream();
            //调用方法读取
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //课程分类列表
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //1.查询出所有的一级分类
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("parent_id","0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(queryWrapper1);
        //2.查询出所有的二级分类
        QueryWrapper queryWrapper2 = new QueryWrapper();
        queryWrapper2.ne("parent_id","0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(queryWrapper2);
        //创建一个集合存储最终数据
        List<OneSubject> finalSubjectList = new ArrayList<>();
        //1.封装所有的一级分类
        for (int i = 0; i < oneSubjectList.size(); i++) {
            EduSubject eduSubject1 = oneSubjectList.get(i);

            OneSubject oneSubject = new OneSubject();
//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());

            BeanUtils.copyProperties(eduSubject1,oneSubject);


            //2.封装所有的二级分类
            List<TwoSubject> twoFinalList = new ArrayList<>();
            for (int j = 0; j < twoSubjectList.size() ; j++) {
                EduSubject eduSubject2 = twoSubjectList.get(j);

                String parentId = eduSubject2.getParentId();
                if (parentId.equals(eduSubject1.getId()) ){
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(eduSubject2,twoSubject);
                    twoFinalList.add(twoSubject);
                }
            }


            oneSubject.setChildren(twoFinalList);
            finalSubjectList.add(oneSubject);

        }

        return finalSubjectList;
    }
}
